package componentInit

import (
	"db2s/go-log/log"
	mq "db2s/topic-mq"
)

var WLog *log.Logger

// struct check topic
const (
	GetTableColumn       Topic = "GetTableColumn"
	TableColumnName      Topic = "TableColumnName"
	TableColumnType      Topic = "TableColumnType"
	TableColumnNull      Topic = "TableColumnNull"
	TableColumnCharset   Topic = "TableColumnCharset"
	TableColumnCollation Topic = "TableColumnCollation"
	TableColumnDefault   Topic = "TableColumnDefault"
	TableColumnComment   Topic = "TableColumnComment"
	TableColumnDatafix   Topic = "TableColumnDatafix"
	TableDatafixApply    Topic = "TableDatafixApply"
)

// index check topic
const (
	GetIndex          Topic = "GetIndex"
	IndexDispos       Topic = "IndexDispos"
	PriIndexDiff      Topic = "PriIndexDiff"
	UniIndexDiff      Topic = "UniIndexDiff"
	NormalIndexDiff   Topic = "NormalIndexDiff"
	IndexApplyDataFix Topic = "IndexApplyDataFix"
)

// partition check topic
const (
	GetPartition   Topic = "GetPartition"
	ParititionDiff Topic = "ParititionDiff"
)

// trigger check topic
const (
	GetTrigger  Topic = "GetTrigger"
	TriggerDiff Topic = "TriggerDiff"
)

// func check topic
const (
	GetFunc  Topic = "GetFunc"
	FuncDiff Topic = "FuncDiff"
)

// proc check topic
const (
	GetProc  Topic = "GetProc"
	ProcDiff Topic = "ProcDiff"
)

const (
	GetHotSql         Topic = "GetHotSql"
	GetHotSqlWrite    Topic = "GetHotSqlWrite"
	GetHotSqlPlayBack Topic = "GetHotSqlPlayBack"
)

// foreign check topic
const (
	GetForeign  Topic = "GetForeign"
	ForeignDiff Topic = "ForeignDiff"
)

// rows index check topic
const (
	IsSegmentQuery       Topic = "IsSegmentQuery"
	IndexConcurrentQuery Topic = "IndexConcurrentQuery"
	QueryTableSql        Topic = "QueryTableSql"
	QueryTableData       Topic = "QueryTableData"
	Md5Rows              Topic = "Md5Rows"
	Arrcmp               Topic = "Arrcmp"
	AbnormalDataDiff     Topic = "AbnormalDataDiff"
	DataFixDiff          Topic = "DataFixDiff"
)
const (
	syncIsSegmentQuery       Topic = "sycIsSegmentQuery"
	syncMissSegmentQuery     Topic = "syncMissSegmentQuery"
	syncIndexConcurrentQuery Topic = "syncIndexConcurrentQuery"
	syncQueryTableSql        Topic = "syncQueryTableSql"
	syncQueryTableData       Topic = "syncQueryTableData"
	syncGenerlInsert         Topic = "syncGenerlInsert"
	syncWriteInsert          Topic = "syncWriteInsert"
)

// rows miss index check topic
const (
	RowsMissQuerySql         Topic = "RowsMissQuerySql"
	RowsMissQueryData        Topic = "RowsMissQueryData"
	RowsMissMd5              Topic = "RowsMissMd5"
	RowsMissArrcmp           Topic = "RowsMissArrcmp"
	RowsMissMd5ValueUniq     Topic = "RowsMissMd5ValueUniq"
	RowsMissAbnormalDataDiff Topic = "RowsMissAbnormalDataDiff"
	RowsMissDataFixSql       Topic = "RowsMissDataFixSql"
	RowsMissAbnormaldataRead Topic = "RowsMissAbnormaldataRead"
	RowsMissFixSqlExec       Topic = "RowsMissFixSqlExec"
)

const (
	Linsert Logo = "/*insertS*/"
	Ldelete Logo = "/*deleteS*/"
)

const (
	Ipri IndexType = "pri"
	Iuni IndexType = "uni"
	Imui IndexType = "mui"
)
const (
	DatafixSql   Event = "[datafixSql]"
	ArrmapE      Event = "[Arrmap]"
	Md5RowsE     Event = "[Md5Rows]"
	QuryRowDataE Event = "[QueryRowsData]"
)
const (
	AbnormalSql      Keywords = "/*abnormalSql*/"
	SdriveSql        Keywords = "/*sdrive*/"
	DdriveSql        Keywords = "/*ddrive*/"
	LimitWhere       Keywords = "/*limit-where*/"
	SDSplite         Keywords = "/*source-dest*/"
	RowsDataSplict   Keywords = "/*go actions rowData*/"
	ColumnDataSplict Keywords = "/*go actions columnData*/"
)
const (
	Count      CheckObject = "count"
	Rows       CheckObject = "rows"
	Sample     CheckObject = "sample"
	Struct     CheckObject = "struct"
	Index      CheckObject = "index"
	Partitions CheckObject = "partitions"
	Foreign    CheckObject = "foreign"
	Trigger    CheckObject = "trigger"
	Func       CheckObject = "func"
	Proc       CheckObject = "proc"
	HotSql     CheckObject = "hotsql"
	SyncRows   CheckObject = "sync"
	IncData    CheckObject = "inc"
)

const (
	CountM        CheckMode = "count"
	RowsIndex     CheckMode = "rowsIndex"
	RowsM         CheckMode = "rows"
	RowsNoIndex   CheckMode = "rowsNoIndex"
	SampleIndex   CheckMode = "SampleIndex"
	SampleNoIndex CheckMode = "SampleNoIndex"
	StructM       CheckMode = "struct"
	IndexM        CheckMode = "index"
	PartitionM    CheckMode = "partitions"
	ForeignM      CheckMode = "foreign"
	TriggerM      CheckMode = "trigger"
	FuncM         CheckMode = "func"
	ProcM         CheckMode = "proc"
	HotSqlM       CheckMode = "hotsql"
	SyncRowsM     CheckMode = "sync"
)

const (
	// IsSegmentQueryMessageQueueLength  Topic IsSegmentQuery 默认容量 用户配置为空 取值 //top对应的管道数量
	IsSegmentQueryMessageQueueLength int = 1
	// IndexConcurrentQueryMessageQueueLength Topic IndexConcurrentQuery 默认容量 用户配置为空 取值
	IndexConcurrentQueryMessageQueueLength = IsSegmentQueryMessageQueueLength * 10
	// DataFixDiffMessageQueueLength Topic DataFixDiff 默认容量 用户配置为空 取值
	DataFixDiffMessageQueueLength = IsSegmentQueryMessageQueueLength * 5
	// ConcurrentSummarySegmentQueryMessageQueueLength Topic ConcurrentSummarySegmentQuery 默认容量 用户配置为空 取值
	ConcurrentSummarySegmentQueryMessageQueueLength = IndexConcurrentQueryMessageQueueLength * 10
	// ConcurrentMissIndexQueryMessageQueueLength Topic ConcurrentMissIndexQueryMessageQueueLength 默认容量 用户配置为空 取值
	ConcurrentMissIndexQueryMessageQueueLength = IsSegmentQueryMessageQueueLength
	// RowsMissIndexQueryMessageQueueLength Topic RowsMissIndexQueryMessageQueueLength 默认容量 用户配置为空 取值
	RowsMissIndexQueryMessageQueueLength = IsSegmentQueryMessageQueueLength

	//	DefaultMessageCapLength = 5  	// DefaultMessageCapLength 默认通道容量 //topic对应的单个管道容量，topic总数=单个容量*管道数量
	// DefaultConsumerPoolSize 监听线程数
	DefaultConsumerPoolSize = 10
)

var (
	checkType map[CheckMode][]*MQTopicListeners
)

type (
	// Topic 自定义Topic 类型 限制范围 避免出错
	Topic string
	Logo  string //dataFix Type 标识
	// CheckObject 自定义CheckObject 类型 限制范围 避免出错
	CheckObject string

	CheckMode string // CheckMode
	IndexType string //Index type distinction
	Keywords  string //data split keyword
	Event     string

	MQConfig struct {
		//mq Topic 配置
		TopicConfig map[Topic]*TopicConfig
		//总执行线程数
		PoolSize int
	}
	// TopicConfig MessageQueueLength MessageCapLength 说明
	// MessageQueueLength * MessageCapLength 为 基础执行线程数 不易设置过大
	// 可以通过 消费者PoolSize 为总线程数控制,会自动调控
	TopicConfig struct {
		//topic类型 必填
		Topic Topic
		//队列长度 不易设置过大 默认 5
		MessageQueueLength int
		//队列缓存容量 默认 5
		MessageCapLength int
		//Topic 队列选择器 //默认随机
		Selector mq.MsgQueueSelector
	}

	//MQTopicListeners Topic 最大可用线程数 PoolSize（） + （MessageQueueLength * MessageCapLength）
	MQTopicListeners struct {
		TopicName Topic
		//队列长度
		MessageQueueLength int
		//队列缓存容量
		MessageCapLength int
		//顺序
		Order int
		//扩充线程数 默认的线程数
		PoolSize int
		//监听任务
		Listeners func(message mq.Message) mq.ConsumeConcurrentlyStatus
	}
	//AutoCloseMqComponent 自动关闭mq的组件
	AutoCloseMqComponent struct {
		TopicStatistics  map[Topic]*int64
		UnsubscribeTopic []Topic
		CheckMode        CheckMode
		EndMark          bool
	}
)
